<?php
session_start();
if(!empty($_SESSION["userType"]))
  {
	$userType = $_SESSION["userType"];
  }

	include 'conn.php';
	
	$page = isset($_POST['page']) ? intval($_POST['page']) : 1;
	$rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
	$NRIC = isset($_POST['NRIC']) ? mysql_real_escape_string($_POST['NRIC']) : '';
        $CustomerName = isset($_POST['CustomerName']) ? mysql_real_escape_string($_POST['CustomerName']) : '';
	$CentreId = isset($_POST['CentreLocation']) ? mysql_real_escape_string($_POST['CentreLocation']) : '';
	//$CentreId = intval($_REQUEST['CentreLocation']);
	$SignInTimeStamp = isset($_POST['SignInTimeStamp']) ? mysql_real_escape_string($_POST['SignInTimeStamp']) : '';
	if($CentreId==0)
	{
		$CentreId="";
	}
	$trimmed = trim($SignInTimeStamp);
	
	$offset = ($page-1)*$rows;
	
	$result = array();
	if($userType=="System Admin")
	{
		$rs = mysql_query("select count(*)
	FROM attendancerecord a, client, centre
	WHERE a.NRIC = client.NRIC AND a.CentreId = Centre.CentreId
	AND a.NRIC LIKE  '%$NRIC%'
            AND client.CustomerName like '%$CustomerName%'
	AND a.SignInTimeStamp LIKE  '%$SignInTimeStamp%'
	AND centre.CentreId LIKE  '%$CentreId%'");
		$row = mysql_fetch_row($rs);
		$result["total"] = $row[0];
		
		$rs = mysql_query("select a.AttendanceRecordID, a.NRIC, a.SignInTimeStamp, a.SignOutTimeStamp, a.Status, client.CustomerName, centre.CentreName
	FROM attendancerecord a, client, centre
	WHERE a.NRIC = client.NRIC AND a.CentreId = Centre.CentreId
	AND a.NRIC LIKE  '%$NRIC%'
            AND client.CustomerName like '%$CustomerName%'
	AND a.SignInTimeStamp LIKE  '%$SignInTimeStamp%'
	AND centre.CentreId LIKE  '%$CentreId%'
            ORDER BY a.SignInTimeStamp DESC
	limit $offset,$rows");
		
		$items = array();
		while($row = mysql_fetch_object($rs)){
			array_push($items, $row);
		}
		$result["rows"] = $items;
		
		echo json_encode($result);
		}
		else
		{
		if(!empty($_SESSION["centreName"]))
		  {   
			  $centreName = $_SESSION["centreName"];
		  }
		  
			  
			  $rs = mysql_query("select count(*)
	FROM attendancerecord a, client, centre
	WHERE a.NRIC = client.NRIC AND a.CentreId = Centre.CentreId
	AND a.NRIC LIKE  '%$NRIC%'
            AND client.CustomerName like '%$CustomerName%'
	AND a.SignInTimeStamp LIKE  '%$SignInTimeStamp%'
	AND centre.CentreName ='$centreName'");
		$row = mysql_fetch_row($rs);
		$result["total"] = $row[0];
		
		$rs = mysql_query("select a.AttendanceRecordID, a.NRIC, a.SignInTimeStamp, a.SignOutTimeStamp, a.Status, client.CustomerName, centre.CentreName
	FROM attendancerecord a, client, centre
	WHERE a.NRIC = client.NRIC AND a.CentreId = Centre.CentreId
	AND a.NRIC LIKE  '%$NRIC%'
            AND client.CustomerName like '%$CustomerName%'
	AND a.SignInTimeStamp LIKE  '%$SignInTimeStamp%'
	AND centre.CentreName='$centreName'
            ORDER BY a.SignInTimeStamp DESC
	limit $offset,$rows");
		
		$items = array();
		while($row = mysql_fetch_object($rs)){
			array_push($items, $row);
		}
		$result["rows"] = $items;
		
		echo json_encode($result);
			  
		}
?>